package com.Txunda.fxw.ui.activity.law;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.TextView;

import com.Txunda.fxw.R;
import com.Txunda.fxw.base.utils.util.JSONUtils;
import com.Txunda.fxw.ui.activity.other.LeaveMessageAty;
import com.Txunda.fxw.ui.activity.other.NoticeAty;
import com.Txunda.fxw.ui.adapter.MyRecyclerAdapter;
import com.Txunda.fxw.ui.base.BaseAty;
import com.Txunda.fxw.ui.callback.MyItemClickListener;
import com.Txunda.fxw.ui.http.AboutUs;
import com.Txunda.fxw.ui.http.Office;
import com.Txunda.fxw.ui.toolkit.util.DensityUtil;
import com.Txunda.fxw.ui.toolkit.util.PreferenceUtil;
import com.Txunda.fxw.ui.widget.XCSlideView;
import com.Txunda.fxw.view.RoundedImageView;
import com.bumptech.glide.Glide;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 民事诉讼法
 *
 * @author Administrator
 */
public class Civil_Procedure_LawAty extends BaseAty {

    private Office office;
    @ViewInject(R.id.aty_title3_tv_center)
    private TextView aty_title3_tv_center;


    @ViewInject(R.id.web_content)
    private WebView web_content;
    private WebSettings set;

    // 屏幕宽度
    private int mScreenWidth = 0;
    private XCSlideView mSlideViewLeft;
    private XCSlideView mSlideViewRight;
    private Context mContext;

    private RecyclerView main_recycler;
    private MyItemClickListener mItemClickListener;

    private int[] imgs;
    private TextView tv_comment;
    private TextView tv_weibo;
    private TextView tv_link;
    private String a_id;
    @ViewInject(R.id.img_logo)
    private RoundedImageView img_logo;
    private List<Map<String, String>> recycler_list_data;
    private MyRecyclerAdapter recyclerAdapter;

    private AboutUs aboutUs;
    private String title;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        a_id = getIntent().getExtras().getString("a_id");
        title = getIntent().getExtras().getString("title");
        aty_title3_tv_center.setText(title);
        set = web_content.getSettings();
        set.setTextSize(WebSettings.TextSize.SMALLER);

        showProgressDialog();
        office.articleDetail(a_id, this);
    }

    @Override
    protected int getLayoutResId() {
        return R.layout.aty_civil__procedure__law;
    }

    @Override
    public void onComplete(String requestUrl, String jsonStr) {
        // TODO Auto-generated method stub
        super.onComplete(requestUrl, jsonStr);
        if (requestUrl.contains("articleDetail")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if ("success".equals(map.get("flag"))) {
                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));

                web_content.loadDataWithBaseURL(null, data.get("content"), "text/html", "utf-8", null);
            }
        }
        if (requestUrl.contains("introduce")) {
            Map<String, String> map = JSONUtils.parseKeyAndValueToMap(jsonStr);
            if ("success".equals(map.get("flag"))) {
                Map<String, String> data = JSONUtils.parseKeyAndValueToMap(map.get("data"));
                String microblog = data.get("microblog");   //微博
                String microblog_url = data.get("microblog_url");   //链接地址
                final String url = data.get("url");   //网址
                recycler_list_data = JSONUtils.parseKeyAndValueToMapList(data.get("list"));
                recyclerAdapter.setList(recycler_list_data);
                main_recycler.setAdapter(recyclerAdapter);
                tv_link.setText(url);

                tv_weibo.setText(microblog);


                tv_link.setOnClickListener(new OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Uri uri = Uri.parse(url);
                        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
                        startActivity(intent);

                    }
                });


            }
        }

    }

    @Override
    protected void initialized() {
        img_logo.setVisibility(View.GONE);
        aboutUs = new AboutUs();
        recycler_list_data = new ArrayList<>();
        recyclerAdapter = new MyRecyclerAdapter(Civil_Procedure_LawAty.this);
        String logo = PreferenceUtil.readString(getApplicationContext(), PreferenceUtil.sp_logo);
        Glide.with(this).load(logo).into(img_logo);
        office = new Office();
        imgs = new int[]{R.drawable.icon_yiyuan, R.drawable.icon_yiyuan, R.drawable.icon_yiyuan};


        mContext = this;
        mScreenWidth = DensityUtil.getScreenWidthAndHeight(mContext)[0];
        mSlideViewRight = XCSlideView.create(Civil_Procedure_LawAty.this, XCSlideView.Positon.RIGHT);
        View menuViewRight = LayoutInflater.from(mContext).inflate(R.layout.layout_slideview, null);
        TextView tv_notice = (TextView) menuViewRight.findViewById(R.id.tv_notice);
        main_recycler = (RecyclerView) menuViewRight.findViewById(R.id.main_recycler);
        tv_weibo = (TextView) menuViewRight.findViewById(R.id.tv_weibo);
        tv_comment = (TextView) menuViewRight.findViewById(R.id.tv_comment);
        tv_link = (TextView) menuViewRight.findViewById(R.id.tv_link);
        mSlideViewRight.setMenuView(Civil_Procedure_LawAty.this, menuViewRight);
        mSlideViewRight.setMenuWidth(mScreenWidth * 6 / 9);
        //公告
        tv_notice.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(NoticeAty.class, null);
            }
        });

        tv_comment.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(LeaveMessageAty.class, null);
            }
        });
        tv_weibo.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                showTips(R.drawable.error, "敬请期待");
            }
        });

        // 设置LinearLayoutManager
        main_recycler
                .setLayoutManager(new LinearLayoutManager(Civil_Procedure_LawAty.this, LinearLayoutManager.HORIZONTAL, false));
        main_recycler.setItemAnimator(new DefaultItemAnimator());
        // 设置固定大小
        main_recycler.setHasFixedSize(true);


    }

    @Override
    @OnClick({R.id.img_logo})
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {
            case R.id.img_logo:
                if (!mSlideViewRight.isShow())
                    mSlideViewRight.show();
                showProgressDialog();
                aboutUs.introduce(this);
                break;

            default:
                break;
        }
    }

    @Override
    protected void requestData() {

    }


}
